﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyControls
{
    public partial class THMonitor: UserControl
    {
        public THMonitor()
        {
            InitializeComponent();
        }

        private int titleNum = 1;
        [Category("自定义属性")]
        [Description("站号")]
        [DefaultValue(1)]
        [Browsable(true)]
        public int TitleNum 
        {
            get { return titleNum; }
            set
            {
                lblTitle.Text = value + "号站点";
                titleNum = value;
            }
        }

        [Category("自定义属性")]
        [Description("温度上限")]
        [DefaultValue(100)]
        [Browsable(true)]
        public int TempWarning { get; set; } = 40;

        private readonly int tempBarHeight=92;
        private int temperatureValue;

        [Category("自定义属性")]
        [Description("温度值")]
        [DefaultValue(0)]
        [Browsable(false)]
        public int TemperatureValue
        {
            get { return temperatureValue; }
            set 
            {
                if (value<0||value>100)
                {
                    MessageBox.Show("温度值必须在0-100之间");
                }
                else
                {
                    //实际温度要显示的蓝色条状高度
                    var realValue=(tempBarHeight / 100.0) * value;
                    var showValue = tempBarHeight -Convert.ToInt32(realValue);
                    lblTemperartureBar.Height = showValue;
                    lblTempValue.Text = value.ToString();


                    //判断是否需要警告
                    if (value>=TempWarning)
                    {
                        lblTempValue.BackColor = Color.Red;
                    }
                    else
                    {
                        lblTempValue.BackColor = SystemColors.Control;
                    }
                }
                temperatureValue = value;
            }
        }



        [Category("自定义属性")]
        [Description("湿度上限")]
        [DefaultValue(100)]
        [Browsable(true)]
        public int HumidityWarning { get; set; } = 40;

        private readonly int humidityBarHeight = 156;
        private int humidityValue;
        [Category("自定义属性")]
        [Description("湿度值")]
        [DefaultValue(0)]
        [Browsable(false)]
        public int HumidityValue
        {
            get { return humidityValue; }
            set
            {
                if (value < 0 || value > 100)
                {
                    MessageBox.Show("湿度值必须在0-100之间");
                }
                else
                {
                    //实际温度要显示的蓝色条状高度
                    var realValue = (humidityBarHeight / 100.0) * value;
                    var showValue = humidityBarHeight - Convert.ToInt32(realValue);
                    lblhumidityBar.Height = showValue;
                    lblHumidityValue.Text = value.ToString();


                    //判断是否需要警告
                    if (value >= TempWarning)
                    {
                        lblHumidityValue.BackColor = Color.Red;
                    }
                    else
                    {
                        lblHumidityValue.BackColor = SystemColors.Control;
                    }
                }
                humidityValue = value;
            }
        }


    }
}
